<template>
	<view class="pageview">
		<cl-navbar :title="$t('my_file.setting.index.title')" leftIconSize="40rpx" placeholder autoBack>
		</cl-navbar>
		<view class="pageContent">
			<view class="cellviewGrop">
				<view class="cellview" @click="goPage('locale')">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t('my_file.setting.locale.title')}}</text>
					</view>
					<view class="cellview__right">
						<text class="cellview__right__content">{{locales[locale]}}</text>
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
			</view>
			<view class="cellviewGrop" @click="goPage('regard')">
				<view class="cellview">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("my_file.setting.index.clearChatHistory")}}</text>
					</view>
					<view class="cellview__right">
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
			</view>
			<view class="cellviewGrop" @click="goPage('regard2')">
				<view class="cellview">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t('my_file.setting.index.clearCache')}}</text>
					</view>
					<view class="cellview__right">
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
			</view>
			<view class="cellviewGrop" @click="checkup">
				<view class="cellview">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("my_file.setting.index.update")}}</text>
					</view>
					<view class="cellview__right">
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
			</view>
			<view class="controlsBotton" @click="toggle">
				<text class="cellview__left__title">{{$t("my_file.setting.index.toggle")}}</text>
			</view>
			<view class="controlsBotton" @click="quit">
				<text class="cellview__left__title" style="color: #FF5B7C;">{{$t("my_file.setting.index.logOut")}}</text>
			</view>

			<!-- <view class="bottombtn" @click="quit">
				{{$t('my_file.setting.index.logOut')}}
			</view> -->

			<view
				style="position: fixed;text-align: center;bottom: 120rpx;left: 0;right: 0;font-size: 28rpx;color: #595A66;">
				{{info.name}} v{{info.version}}</view>

		</view>
		<view>
			<cl-model ref="clmodel"></cl-model>
		</view>
	</view>
</template>

<script>
	import msgSqlite from "@/sqlite_services/msg.js"
	import commonApi from "@/services/api/common.js";
	import {
		mapState
	} from 'vuex'
	export default {
		name: 'my_setting',
		data() {
			return {
				locale: 'zh-Hans',
				value: false,
				locales: {
					'en': this.$t('locale.en'),
					'zh-Hans': this.$t('locale.zh-hans')
				},
				info: {}
			}
		},
		computed: {
			...mapState({
				user_info: state => state.user.user_info,
			})
		},
		onLoad() {
			// 获取应用信息
			plus.runtime.getProperty(plus.runtime.appid, (info) => {
				console.log(info);
				this.info = info
			})
		},
		onShow() {
			this.locale = uni.getLocale()
		},
		methods: {
			checkup() {
				// const routes = getCurrentPages();  
				// let curRoute = routes[routes.length - 1].route  
				// console.log(curRoute);
				
				// plus.navigator.createShortcut({
				// 	extra: {
				// 		path: '/pages/login/login', // 应用地址  
				// 		title: '第adacc捷方式'
				// 	},
				// }, function(e) {
				// 	console.log("A=", e); // {"sure":false}  
				// }, function(e) {
				// 	console.log("B=", e);
				// 	console.log("C=", e.message);
				// });
				// return
				commonApi.appUpdate(1)
			},
			toggle(){
				this.$Router.push({
					name: 'my_setting_toggle'
				})
			},
			quit() {
				if(this.user_info.is_set_password == "0"){
					this.$refs.clmodel.show({
						title: this.$t("common.Tips"),
						content: this.$t("my_file.privacySecurity.index.passwordTip"),
						confirm: (e) => {
							e.showModel = false
							this.$Router.push({
								name:"changePassword"
							})
						}
					})
					return
				}
				this.$store.dispatch('logOut')
			},
			goPage(type) {
				switch (type) {
					case 'locale':
						this.$Router.push({
							name: 'my_setting_locale'
						})
						break;
					case 'regard2':
						this.$refs.clmodel.show({
							title: this.$t("my_file.setting.index.clearCache"),
							content: this.$t("my_file.setting.index.clearChatHistoryDetails"),
							confirm: (e) => {
								e.showModel = false
								this.deleteAllFiles()
								this.clearCache()
							}
						})
						break;
					case 'regard':
						this.$refs.clmodel.show({
							title: this.$t("my_file.setting.index.clearChatHistory"),
							content: this.$t("my_file.setting.index.clearCacheDetails"),
							confirm: (e) => {
								e.showModel = false
								msgSqlite.clearChatAllHistory()
							}
						})
						break;
					default:
						break;
				}
			},
			clearCache() {
				let that = this;
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  
									// uni.showToast({
									// 	title: '缓存清理完成',
									// 	duration: 2000
									// });
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							console.log('文件路径读取失败')
						});
					}
				} else { // ios  
					plus.cache.clear(function() {
						// uni.showToast({
						// 	title: '缓存清理完成',
						// 	duration: 2000
						// });
					});
				}
			},
			// 封装获取文件列表和删除文件的异步函数
			getSavedFileList() {
				return new Promise((resolve, reject) => {
					uni.getSavedFileList({
						success: res => resolve(res.fileList),
						fail: err => reject(err)
					});
				});
			},

			removeFile(filePath) {
				return new Promise((resolve, reject) => {
					uni.removeSavedFile({
						filePath: filePath,
						success: () => resolve(),
						fail: err => reject(err)
					});
				});
			},

			// 主函数，使用 async/await 处理异步操作
			async deleteAllFiles() {
				try {
					let Emojis = uni.getStorageSync('local_Emojis')
					uni.removeStorageSync('image_stars_cache_list')
					uni.removeStorageSync('image_chat_cache_list')
					uni.removeStorageSync('video_chat_cache_list')
					uni.removeStorageSync('audio_chat_cache_list')
					uni.removeStorageSync('image_avatar_cache_list')

					const fileList = await this.getSavedFileList();
					for (let file of fileList) {
						let filePath = Emojis.find(item => item.src == file.filePath)
						if (!filePath) {
							await this.removeFile(file.filePath);
						}
					}
					uni.showToast({
						title: this.$t("tost.title.cleanOk"),
						icon: 'none'
					})
				} catch (err) {
					uni.showToast({
						title: this.$t("tost.title.cleanFail"),
						icon: 'none'
					})
				}
			}

		},
	};
</script>

<style scoped lang="scss">
	.bottombtn {
		width: 342rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		font-size: 32rpx;
		margin: 150rpx auto 0;
		color: #FFFFFF;
		background: linear-gradient(270deg, #FF7B3C 0%, #FF5B7C 100%);
		border-radius: 40rpx;
	}
	.controlsBotton{
		height: 110rpx;
		text-align: center;
		line-height: 110rpx;
		background: #1E1F30;
		border-radius: 20rpx;
		margin: 20rpx 30rpx 20rpx;
		font-size: 32rpx;
	}
	.cellviewGrop {
		background: #1E1F30;
		border-radius: 20rpx;
		margin: 20rpx 30rpx 0;

		.cellview {
			display: flex;
			justify-content: space-between;
			padding: 0 30rpx;
			height: 110rpx;

			&__left {
				display: flex;
				align-items: center;


				&__title {
					font-size: 28rpx;
					color: #FFFFFF;
				}
			}

			&__right {
				display: flex;
				align-items: center;

				&__content {
					font-size: 28rpx;
					color: #61626e;
				}
			}
		}
	}
</style>